/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.example.netcracker.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.Timer;

/**
 *
 * @author Nikolay
 */
public class ClockPanel extends javax.swing.JPanel implements ActionListener {

    private final Timer timer;
    
    public ClockPanel(Date date) {
        timer = new Timer(1000, this);
        initComponents();
    }
    
    public ClockPanel() {
        timer = new Timer(1000, this);
        initComponents();        
    }
    
    public void setDateTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss dd.MMMM.yyyy");
        String formatDate = format.format(date);
        String[] split = formatDate.split(" ");
        String timeStr = split[0];
        String dateStr = split[1];
        timeLabel.setText(timeStr);
        dateLabel.setText(dateStr);
    }
    
    public void startClock() {
        timer.start();
    }
    
    public void stopClock() {
        timer.stop();
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        setDateTime(Calendar.getInstance().getTime());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        timeLabel = new javax.swing.JLabel();
        dateLabel = new javax.swing.JLabel();

        timeLabel.setFont(new java.awt.Font("Times New Roman", 0, 72)); // NOI18N
        timeLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        timeLabel.setText("10:10:10");

        dateLabel.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
        dateLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        dateLabel.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(timeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE)
                    .addComponent(dateLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(timeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dateLabel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel dateLabel;
    private javax.swing.JLabel timeLabel;
    // End of variables declaration//GEN-END:variables

}
